package homework;

import java.util.Scanner;
class Person{
    private String name;
    private String sex;
    private int age;
    private boolean isMarried;

    public Person(String name, String sex, int age, boolean isMarried) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.isMarried = isMarried;
    }
    private boolean isEnoughAge(){
        boolean flag=false;
        if(!(this.isMarried)){
            if((this.sex.equals("女") && this.age>=19)||(this.sex.equals("男") && this.age>=20)){
                flag=true;
            } else {
                System.out.println(this.name+"年龄不足或性别有误！");
            }
        }else {
            System.out.println(this.name+"已婚！");
        }
        return flag;
    }
    public static boolean marryWith(Person p1,Person p2){
        //工具类方法，执行p1和p2结婚
        //返回是否结婚成功
        //1 都要未婚
        //2 要异性
        //3 男生到达20岁，女生到达19岁
        boolean flag=false;
        System.out.println("  "+p1.name+"与"+p2.name+"正在结婚");
        if(!(p1.sex.equals(p2.sex))){
            if (p1.isEnoughAge() & p2.isEnoughAge()) {
                System.out.println(p1.name+"与"+p2.name+"结婚成功");
                flag=true;
                //更新状态
                p1.setMarried(true);
                p2.setMarried(true);
            }
        }else {
            System.out.println("只支持异性");
        }
        return flag;
    }
    public void study(String school){}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isMarried() {
        return isMarried;
    }

    public void setMarried(boolean married) {
        isMarried = married;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", isMarried=" + isMarried +
                '}';
    }
}
public class Homework {
    public static void main(String[] args) {
        Person p1=new Person("a","男",20,false);
        Person p2=new Person("b","女",20,false);
        Person p3=new Person("c","男",10,false);
        Person p4=new Person("d","女",20,false);
        Person.marryWith(p1,p2);
        Person.marryWith(p1,p4);
        Person.marryWith(p2,p4);
        Person.marryWith(p3,p4);
        System.out.println("二");
        Person person=new Person("A","男",10,false){
            @Override
            public void study(String school) {
                System.out.println("输入学习时间（年）：");
                int year=new Scanner(System.in).nextInt();
                System.out.println("在"+school+"学校学习了"+year+"年");
            }
        };
        person.study("C");
    }
}
